package com.freedomotic.jfrontend.utils;

import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

/**
 *
 * @author Enrico Nicoletti
 */
public class PropertiesPanel
        extends javax.swing.JPanel {

    private ArrayList<Component> table;
    private int rows;
    private int cols;
    private static final int MAX_ROWS = 100;

    /**
     * Creates new form PropertiesPanel
     *
     * @param rows
     * @param cols
     */
    public PropertiesPanel(int rows, int cols) {
        initComponents();
        this.setVisible(true);
        this.setPreferredSize(new Dimension(500, 500));
        this.rows = rows;
        this.cols = cols;
        table = new ArrayList<Component>();
        this.setLayout(new SpringLayout());
    }

    /**
     *
     * @param component
     * @param row
     * @param col
     */
    public synchronized void addElement(Component component, final int row, final int col) {
        if (component == null) {
            throw new IllegalArgumentException("Cannot add a null component");
        }

        table.add(component);

        if (col == (cols - 1)) { //is the last col
            component.setMaximumSize(new Dimension(2000, 50));
            component.setPreferredSize(new Dimension(200, 50));
        } else {
            component.setMaximumSize(new Dimension(200, 50));
            component.setPreferredSize(new Dimension(200, 50));
        }

        add(component);
    }

    /**
     *
     */
    public void layoutPanel() {
        //Lay out the panel.
        // System.out.println("row:" + rows + " cols:" + cols);
        SpringUtilities.makeCompactGrid(this, rows, cols, //rows, cols
                5, 5, //initX, initY
                5, 5); //xPad, yPad
        this.validate();
    }

    /**
     *
     * @return
     */
    public int addRow() {
        return rows++;
    }

    /**
     *
     * @return
     */
    public int addColumn() {
        return cols++;
    }

    /**
     *
     * @return
     */
    public int getRows() {
        return rows;
    }

    /**
     *
     * @return
     */
    public int getColumns() {
        return cols;
    }

    /**
     *
     * @param row
     * @param col
     * @return
     */
    public String getComponent(int row, int col) {
        Component comp = table.get((row * col) + col);

        if (comp != null) {
            if (comp instanceof JTextField) {
                JTextField jTextField = (JTextField) comp;

                return jTextField.getText();
            } else {
                if (comp instanceof JComboBox) {
                    JComboBox combo = (JComboBox) comp;

                    return combo.getSelectedItem().toString();
                }
            }
        }

        return null;
    }

    /**
     *
     */
    @Override
    public void removeAll() {
        super.removeAll();
        table.clear();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 296, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
